/*
 * Copyright (c) 2011, Eriq Muhammad Adams J.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Institute Technology of Sepuluh Nopember nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ERIQ MUHAMMAD ADAMS J. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.pathmachine.ui.renderer.impl;

import com.googlecode.pathmachine.mm.Map;
import com.googlecode.pathmachine.mm.Point2D;
import com.googlecode.pathmachine.mm.Point3D;
import com.googlecode.pathmachine.mm.TTerrain;
import com.googlecode.pathmachine.ui.renderer.GridRenderer;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import java.util.ArrayList;
import java.util.List;
import jme3tools.optimize.GeometryBatchFactory;

/**
 *
 * @author <a href="mailto:eriq.adams@gmail.com">Eriq Muhammad Adams J.</a> 
 */
public class GridRendererImpl implements GridRenderer {

    private ColorRGBA edgeColor = ColorRGBA.Yellow;
    private ColorRGBA nodeColor = ColorRGBA.Pink;

    public Node render(Map map, boolean diagonalSupport, boolean edgesDrawing, AssetManager assetManager) {
        Node node = new Node("mapGridDebug");

        List<Point3D> startPoints = new ArrayList<Point3D>();
        List<Point3D> endPoints = new ArrayList<Point3D>();
        List<Geometry> nodeGeoms = new ArrayList<Geometry>();
        int numPoints = 0;
        int numNodes = 0;
        for (int i = 0; i < map.getLand().size(); i++) {
            Point2D p2 = map.getCoordinate(i);
            int x = p2.x, y = p2.y;
            if (TTerrain.kGround == map.getTerrainType(x, y)) {
                Point3D sp = map.getOpenGLCoord(x, y);
                // draw grid nodes
                float size = 0.01f;
                Box nodeBox = new Box(size, size, size);
                Geometry nodeGeom = new Geometry("g", nodeBox);
                nodeGeom.setLocalTranslation(sp.x, sp.y + size, sp.z);
                nodeGeoms.add(nodeGeom);
                numNodes++;

                if (edgesDrawing) {

                    // add points
                    if (((x + 1) < map.getMapWidth() && TTerrain.kGround == map.getTerrainType(x + 1, y))) {
                        addPoints(map, x + 1, y, sp, startPoints, endPoints);
                        numPoints++;
                    }

                    if ((y + 1) >= 0 && TTerrain.kGround == map.getTerrainType(x, y + 1)) {
                        addPoints(map, x, y + 1, sp, startPoints, endPoints);
                        numPoints++;
                    }

                    if (diagonalSupport) {
                        if ((x + 1) < map.getMapWidth() && (y - 1) >= 0 && TTerrain.kGround == map.getTerrainType(x + 1, y - 1)) {
                            addPoints(map, x + 1, y - 1, sp, startPoints, endPoints);
                            numPoints++;
                        }
                        if ((x + 1) < map.getMapWidth() && (y + 1) < map.getMapHeight() && TTerrain.kGround == map.getTerrainType(x + 1, y + 1)) {
                            addPoints(map, x + 1, y + 1, sp, startPoints, endPoints);
                            numPoints++;
                        }
                    }
                }
            }
        }


        // Merge node geoms into single mesh
        Mesh nodeMesh = new Mesh();
        GeometryBatchFactory.mergeGeometries(nodeGeoms, nodeMesh);
        nodeGeoms.clear();
        nodeGeoms = null;
        Geometry nodeGeom = new Geometry("nodeMesh", nodeMesh);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.getAdditionalRenderState().setWireframe(false);
        mat.setColor("Color", nodeColor);
        nodeGeom.setMaterial(mat);
        node.attachChild(nodeGeom);


        if (edgesDrawing) {
            // Draw Grid Edges
            Edges edges = new Edges(startPoints, endPoints);
            Geometry edgesGeom = new Geometry("edges", edges);
            edgesGeom.move(0f, +0.01f, 0f);
            Material edgesMat = mat.clone();
            edgesMat.setColor("Color", edgeColor);
            edgesGeom.setMaterial(edgesMat);
            node.attachChild(edgesGeom);
            startPoints = null;
            endPoints = null;
        }

        return ((Node) GeometryBatchFactory.optimize(node));
    }

    private void addPoints(Map map, int x, int y, Point3D sp, List<Point3D> startPoints, List<Point3D> endPoints) {
        Point3D ep = map.getOpenGLCoord(x, y);
        startPoints.add(sp);
        endPoints.add(ep);
    }

    public void setEdgeColor(ColorRGBA color) {
        edgeColor = color;
    }

    public void setNodeColor(ColorRGBA color) {
        nodeColor = color;
    }
}
